import {Component, OnInit, inject, OnDestroy} from '@angular/core';
import {CommonModule} from '@angular/common';
import {WorkspaceComponent} from '../workspace/workspace.component';
import {LayoutService} from '../../services/layout.service';
import {TransformService} from '../../services/transform.service';
import {UndoRedoService} from '../../services/undo-redo.service';
import {SelectionService} from '../../services/selection.service';
import {TablesService} from '../../services/tables.service';
import {AreasService} from '../../services/areas.service';
import {NotesService} from '../../services/notes.service';
import {TypesService} from '../../services/types.service';
import {EnumsService} from '../../services/enums.service';
import {TasksService} from '../../services/tasks.service';
import {SaveStateService} from '../../services/save-state.service';

@Component({
  selector: 'app-editor',
  standalone: true,
  imports: [CommonModule, WorkspaceComponent],
  template: `
    <div class="h-screen w-screen overflow-hidden bg-gray-50">
      <app-workspace></app-workspace>
    </div>`,
  styles: `
    :host {
      display: block;
      height: 100vh;
      width: 100vw;
    }`
})
export class EditorComponent implements OnInit, OnDestroy {
  // 注入所有必要的服务，替代原来的 Context Providers
  layoutService = inject(LayoutService);
  transformService = inject(TransformService);
  undoRedoService = inject(UndoRedoService);
  selectionService = inject(SelectionService);
  tablesService = inject(TablesService);
  areasService = inject(AreasService);
  notesService = inject(NotesService);
  typesService = inject(TypesService);
  enumsService = inject(EnumsService);
  tasksService = inject(TasksService);
  saveStateService = inject(SaveStateService);

  private keydownListener?: (event: KeyboardEvent) => void;

  ngOnInit() {
    this.setupKeyboardShortcuts();
    this.initializeEditor();
  }

  ngOnDestroy() {
    if (this.keydownListener) {
      document.removeEventListener('keydown', this.keydownListener);
    }
  }

  private initializeEditor() {
    console.log('Editor initialized with Angular 20');
    // 设置初始状态
    this.saveStateService.setCurrentProject('新项目');
  }

  private setupKeyboardShortcuts() {
    this.keydownListener = (event: KeyboardEvent) => {
      if (event.ctrlKey || event.metaKey) {
        switch (event.key) {
          case 'z':
            if (event.shiftKey) {
              this.undoRedoService.redo();
            } else {
              this.undoRedoService.undo();
            }
            event.preventDefault();
            break;
          case 'y':
            this.undoRedoService.redo();
            event.preventDefault();
            break;
          case 's':
            this.saveProject();
            event.preventDefault();
            break;
          case 'n':
            this.createNewProject();
            event.preventDefault();
            break;
        }
      }

      if (event.key === 'Escape') {
        this.selectionService.clearSelection();
      }
    };

    document.addEventListener('keydown', this.keydownListener);
  }

  private saveProject() {
    // 保存项目逻辑
    this.saveStateService.markSaved();
    console.log('Project saved');
  }

  private createNewProject() {
    // 创建新项目逻辑
    this.tablesService.clearTables();
    this.notesService.clearNotes();
    this.areasService.clearAreas();
    this.typesService.clearTypes();
    this.enumsService.clearEnums();
    this.tasksService.clearTasks();
    this.saveStateService.reset();
    this.transformService.reset();
    console.log('New project created');
  }
}
